﻿using OperationPlugins.Applications.Remoting;
using OperationPlugins.Validators;
using System;

namespace OperationPlugins.Applications
{
    /// <summary>
    /// This is the default IValidatorProxy implementation.
    /// </summary>
    internal sealed class ValidatorProxyImpl : ProxyBase, IValidatorProxy
    {
        internal ValidatorProxyImpl(ApplicationInstanceImpl instance) : base(instance)
        {
        }

        /// <summary>
        /// Proceeds the validation.
        /// </summary>
        /// <returns>A container for validation result messages.</returns>
        public MessageContainer Validate()
        {           
            SandboxContext parameters = base.PrepareRequest();

            try
            {
                SandboxRunnerReturnValue rv = (SandboxRunnerReturnValue)base.Instance.Sandbox.Runner.Invoke(parameters);
                return rv.Messages;
            }
            catch (Exception e)
            {                
                throw new ValidatorProxyException("An unhandled exception occurred inside the sandbox. Please check the logs for more details.", e, false);
            }
        }

        public void Dispose()
        {
        }

    }
}
